unit razao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons, Mask;

type
  TFrazao = class(TForm)
    DBGrid1: TDBGrid;
    Label1: TLabel;
    MaskEdit1: TMaskEdit;
    Edit1: TEdit;
    DataSource1: TDataSource;
    MaskEdit2: TMaskEdit;
    MaskEdit3: TMaskEdit;
    Label2: TLabel;
    Label3: TLabel;
    razao: TTable;
    Edit2: TEdit;
    razaoData: TDateField;
    razaoConta: TStringField;
    razaoHp: TFloatField;
    razaoComplemento: TStringField;
    razaoValor: TFloatField;
    razaoSaldo: TFloatField;
    Button1: TButton;
    procedure FormShow(Sender: TObject);
    procedure MaskEdit1Exit(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Frazao: TFrazao;
  msaldo: real;
implementation

uses Dados;




{$R *.dfm}


procedure TFrazao.FormShow(Sender: TObject);
begin
   MaskEdit1.SetFocus;

end;


procedure TFrazao.MaskEdit1Exit(Sender: TObject);
begin
if not fdados.contas.FindKey([maskedit1.text]) then
   begin
   beep;
   showmessage('Conta nao cadastrada');
   maskedit1.SetFocus;
   exit;
   end;
   msaldo := fdados.contassaldoinicial.value;
   edit1.Text := fdados.contasnome.value;
   edit2.Text := fdados.ContasSaldoinicial.AsString;
   button1.SetFocus;
end;

procedure TFrazao.FormClose(Sender: TObject; var Action: TCloseAction);
begin
razao.close;
razao.exclusive := true;
if razao.exclusive then
   begin
   beep;
   razao.open;
   razao.EmptyTable;
   razao.close;
end;

end;
procedure TFrazao.Button1Click(Sender: TObject);
var
mcredora, mdevedora, mdata : string;
begin
razao.open;
razao.append;
razaocomplemento.value := '*** Saldo inicial ***';
razaovalor.value       := msaldo;


fdados.Diario.First;
while not fdados.diario.eof do
   begin
   mdata := datetostr(fdados.DiarioData.Value);
   if copy(mdata,7,4) <> '2005' then
      begin
      fdados.Diario.Next;
      continue;
   end;
   mdevedora := fdados.diariodevedora.value;
   mcredora  := fdados.diariocredora.value;
   if maskedit1.Text <> mdevedora then
      begin
      if maskedit1.text <> mcredora then
         begin
         fdados.diario.Next;
         continue;
      end;
   end;
   razao.append;
   razaodata.value := fdados.diariodata.value;
   razaohp.value   := fdados.diariohp.value;
   razaocomplemento.value := fdados.diariocomplemento.value;
   if maskedit1.Text = mcredora then
      begin
      beep;
      msaldo := msaldo - fdados.diariovalor.value;
      razaoconta.value := fdados.diariodevedora.value;
      razaovalor.value := fdados.diariovalor.value * -1;
      // * -1;
      end;
   if maskedit1.Text = mdevedora then
      begin
      msaldo := msaldo + fdados.diariovalor.value;
      razaoconta.value := fdados.diariocredora.value;
      razaovalor.value := fdados.diariovalor.value;
      end;
   razaosaldo.value := msaldo;
   razao.post;
   fdados.Diario.Next;
 end;
razao.first;
maskedit1.setfocus;
edit2.Text := inttostr(razao.recno);
edit2.text := mcredora;
end;
end.
